package org.adorsys.aderp.orga.web;

import java.util.List;

import org.adorsys.aderp.orga.domain.OrgaPerson;
import org.adorsys.aderp.orga.domain.OrgaRelation;
import org.adorsys.aderp.orga.domain.PersonAddress;
import org.adorsys.aderp.orga.domain.PersonEmail;
import org.adorsys.aderp.orga.domain.ResourceRelation;
import org.adorsys.aderp.orga.service.OrgaRelationService;
import org.adorsys.aderp.orga.service.PersonAddressService;
import org.adorsys.aderp.orga.service.PersonEmailService;
import org.adorsys.aderp.orga.service.ResourceRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.roo.addon.web.mvc.controller.scaffold.RooWebScaffold;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@RequestMapping("/orgapeople")
@Controller
@RooWebScaffold(path = "orgapeople", formBackingObject = OrgaPerson.class)
public class OrgaPersonController {

	@Autowired
	private PersonAddressService personAddressService;
	
	@Autowired
	private PersonAddressController personAddressController;

	@Autowired
	private OrgaRelationController orgaRelationController;
	
	@Autowired
	private OrgaRelationService orgaRelationService;
	
	@Autowired
	private PersonEmailController personEmailController;
	
	@Autowired
	private PersonEmailService personEmailService;
	
	@Autowired
	private ResourceRelationController resourceRelationController;
	
	@Autowired
	private ResourceRelationService resourceRelationService;

	
	
	@RequestMapping(value = "/{id}", produces = "text/html")
    public String show(@PathVariable("id") Long id, Model uiModel) {
		addDateTimeFormatPatterns(uiModel);
        OrgaPerson orgaPerson = orgaPersonService.findOrgaPerson(id);
        uiModel.addAttribute("orgaperson", orgaPerson);
        uiModel.addAttribute("itemId", id);
        listSourceRelations(id, 0, 5, uiModel);        
        
        List<PersonAddress> personAddresses = personAddressService.findByOrgaPerson(orgaPerson);
        uiModel.addAttribute("personaddresses", personAddresses);
        personAddressController.addDateTimeFormatPatterns(uiModel);

        List<PersonEmail> personEmails = personEmailService.findByOrgaPerson(orgaPerson);
        uiModel.addAttribute("personemails", personEmails);
        personEmailController.addDateTimeFormatPatterns(uiModel);

        return "orgapeople/show";
    }
    
    private String listSourceRelations(Long personHolderId,
    		Integer page, 
    		Integer size, 
    		Model uiModel) 
    {
    	
		OrgaPerson person = personHolderId!=null? orgaPersonService.findOrgaPerson(personHolderId):null;

    	Page<OrgaRelation> personPage = orgaRelationService.searchOrgaRelations(person, null, null,page, size);
    	 Page<ResourceRelation> resourceRelationsPage = resourceRelationService.searchResourceRelation(person, null, null, page, size);

         uiModel.addAttribute("resourcerelations", resourceRelationsPage.getContent());
         resourceRelationController.addDateTimeFormatPatterns(uiModel);
         
        uiModel.addAttribute("orgarelations", personPage.getContent());
        orgaRelationController.addDateTimeFormatPatterns(uiModel);
        return "orgarelations/list";
    }
}
